<html>
<head>
    <title>机构管理</title>
    <#include "/common/import.ftl" >
    <script type="text/javascript" src="${ctx}/js/easyui/extendvalidate.js"></script>
    <script>
        $(function () {
            $('#orgList').treegrid({
                title: '机构列表',
                iconCls: 'icon-ok',
                fitColumns: true,
                nowrap: false,
                rownumbers: true,
                animate: true,
                collapsible: false,
                url: '${ctx}/uct/org/org!data.action?id=${id!""}',
                idField: 'orgId',
                treeField: 'orgCname',
                frozenColumns: [[
                    {
                        title: '机构名称', field: 'orgCname', width: 200,
                        formatter: function (value) {
                            return '<span style="color:blue">' + value + '</span>';
                        }
                    }
                ]],
                columns: [[
                    {
                        field: 'orgEname', title: '机构标识', width: 100, sortable: true,
                        sorter: function (a, b) {
                            return (a > b ? 1 : -1);
                        }
                    },
                    {field: 'orgDomain', title: '所属域', width: 80},
                    {
                        field: 'subCount', title: '子节点数', width: 80,
                        formatter: function (value) {
                            if (value && value > 0) {
                                return value;
                            } else {
                                return '0';
                            }
                        }
                    },
                    {field: 'fullEname', title: '英文全称', width: 150},
                    {field: 'fullCname', title: '中文全称', width: 150},
                    {
                        field: 'orgStatus', title: '当前状态', width: 80,
                        formatter: function (value) {
                            if (value == '1') {
                                return '<font color=blue>启用</font>';
                            } else {
                                return '<font color=red>停用</font>';
                            }
                        }
                    }

                ]],
                toolbar: [
                    {
                        text: '增加机构',
                        iconCls: 'icon-add',
                        handler: function () {
                            addRootOrg();
                        }

                    }
                    , '-',
                    {
                        text: '增加子机构',
                        iconCls: 'icon-add',
                        handler: function () {
                            addSubOrg();
                        }
                    }
                    , '-',
                    {
                        text: '删除',
                        iconCls: 'icon-cut',
                        handler: deleteOrg
                    }, '-',
                    {
                        text: '修改',
                        iconCls: 'icon-edit',
                        handler: function () {
                            editOrg();
                        }
                    }
                    , '-',
                    {
                        text: '刷新',
                        iconCls: 'icon-reload',
                        handler: function () {
                            reload();
                        }
                    }, '-',
                    {
                        text: '部门管理',
                        handler: function () {
                            deptList();
                        }
                    }, '-',
                    {
                        text: '机构人员',
                        handler: function () {
                            deptUserList();
                        }
                    }, '-',
                    {
                        text: '角色设置',
                        handler: function () {
                            orgRole();
                        }
                    }, '-',
                    {
                        text: '资源设置',
                        handler: function () {
                            actSeting();
                        }
                    }
                ],
                onBeforeLoad: function (row, param) {
                    if (row) {
                        $(this).treegrid('options').url = '${ctx}/uct/org/org!data.action?fatherId=';
                    }
                },
                onDblClickRow: function (row) {
                    editOrg();
                }
            });
        });

        var widthcount = $(document.body).width() - 20;
        var leftcount = document.documentElement.scrollLeft;

        function addRootOrg(paraFlag) {
            $('#orgAddFrame').html('');
            var url = '${ctx}/uct/org/add?fatherId=0';
            $('#orgAddFrame').attr("src", url);
            $('#orgAddWin').window('open');
        }

        function addSubOrg(paraFlag) {
            $('#orgAddFrame').html('');
            var node = getSelected();
            if (node) {
                var url = '${ctx}/uct/org/add?fatherId=' + node.orgId;
                $('#orgAddFrame').attr("src", url);
                $('#orgAddWin').window('open');
            }
        }

        function orgRole() {
            var node = getSelected();
            if (node) {
                var orgId = node.orgId;
                var deptListUrl = '${ctx}/uct/role/list.action?orgId=' + orgId;
                if ($.isFunction(window.parent.addTab)) {
                    window.parent.addTab('[' + node.orgCname + ']角色管理', deptListUrl, 'icon-dept');
                } else {
                    $('#orgDeptWin').css({
                        "width": widthcount,
                        "height": "450px",
                        "left": leftcount
                    });
                    $('#orgDeptFrame').attr("src", deptListUrl);
                    $('#orgDeptWin').panel('setTitle', "[" + node.orgCname + "]角色管理");
                    $('#orgDeptWin').window('open');
                }
            }
        }

        function editOrg() {
            var node = getSelected();
            if (node) {
                var url = '${ctx}/uct/org/add?orgId=' + node.orgId;
                $('#orgAddFrame').attr("src", url);
                $('#orgAddWin').window('open');
            }
        }

        function deptList() {
            var node = getSelected();
            if (node) {
                var orgId = node.orgId;
                var deptListUrl = '${ctx}/uct/dept/list.action?orgId=' + orgId;
                if ($.isFunction(window.parent.addTab)) {
                    window.parent.addTab('部门管理[' + node.orgCname + ']', deptListUrl, 'icon-dept');
                } else {
                    $('#orgDeptWin').css({
                        "width": widthcount,
                        "height": "450px",
                        "left": leftcount
                    });
                    $('#orgDeptFrame').attr("src", deptListUrl);
                    $('#orgDeptWin').panel('setTitle', "部门管理[" + node.orgCname + "]");
                    $('#orgDeptWin').window('open');
                }
            }
        }

        function deptUserList() {
            var node = getSelected();
            if (node) {
                var orgId = node.orgId;
                var deptUserListUrl = '${ctx}/uct/org/org-user-list?orgId=' + orgId;
                if ($.isFunction(window.parent.addTab)) {
                    parent.addTab('机构用户列表[' + node.orgCname + ']', deptUserListUrl);
                } else {
                    $('#orgDeptWin').css({
                        "width": widthcount,
                        "height": "450px",
                        "left": leftcount
                    });
                    $('#orgDeptFrame').attr("src", deptUserListUrl);
                    $('#orgDeptWin').panel('setTitle', '机构用户列表[' + node.orgCname + ']');
                    $('#orgDeptWin').window('open');
                }
            }
        }

        //设置权限
        function actSeting() {
            var node = getSelected();
            if (node) {
                var orgId = node.orgId;
                $.ajax({
                    type: "post",
                    url: '${ctx}/uct/org/org-act!checkAct.action?',
                    success: function (data, textStatus) {
                        var result = jQuery.parseJSON(data);
                        if (result.flag == 'T') {
                            if ("1" != "${sysUser}" && node.orgEname == "${loginUser.orgEname}") {
                                $.messager.alert('提交结果', "不能给自己机构设置资源", 'info');
                            } else {
                                var url = '${ctx}/uct/org/org-act.action?orgId=' + orgId;
                                $('#orgDeptWin').css({
                                    "width": widthcount,
                                    "height": "450px",
                                    "left": leftcount
                                });
                                $('#orgDeptFrame').attr("src", url);
                                $('#orgDeptWin').panel('setTitle', '机构资源[<font color=red>' + node.orgCname + '</font>]');
                                $('#orgDeptWin').window('open');
                            }
                        } else {
                            $.messager.alert('提交结果', result.msg, 'error');
                        }
                    }
                });
            }
        }

        //删除
        function deleteOrg() {
            var node = getSelected();
            if (node) {
                $.messager.confirm('确认', '您确定要删除:<font color="red">' + node.orgCname + '</font> ？', function (r) {
                    if (r) {
                        $.ajax({
                            type: "post",
                            url: "${ctx}/uct/org/del.action?id=" + node.orgId,
                            success: function (data, textStatus) {
                                var result = jQuery.parseJSON(data);
                                if (result.flag == 'T') {
                                    $.messager.alert('提交结果', '操作成功', 'info', function () {
                                        $('#orgList').treegrid('remove', node.orgId);
                                        if ("0" == node.fatherId) {
                                            reload();
                                        } else {
                                            reloadTree(node.fatherId, '');
                                        }
                                    });
                                } else {
                                    $.messager.alert('提交结果', "操作失败：" + result.msg, 'error');
                                }
                            }
                        });
                    }
                });
            }
        }

        function getSelected() {
            var selected = $('#orgList').treegrid('getSelected');
            if (selected) {
                return selected;
            } else {
                $.messager.alert('提示', '请选择要操作的数据', 'info');
            }
        }

        //刷新
        function reload() {
            var node = $('#orgList').treegrid('getSelected');
            if (node && node.fatherId != "0") {
                $('#orgList').treegrid('reload', node.orgId);
            } else {
                $('#orgList').treegrid('reload');
            }
        }

        //刷新树
        function reloadTree(fatherId, nodeId) {
            if (nodeId == "") {
                var parent = $('#orgList').treegrid('getParent', fatherId);
                if (parent) {
                    var parentId = parent["orgId"];
                    $('#orgList').treegrid('reload', parentId);
                } else {
                    $('#orgList').treegrid('reload');
                }
            } else {
                $('#orgList').treegrid('reload', fatherId);
            }
        }

        function closeOrgWindow() {
            $("#orgAddWin").window("close");
        }
    </script>
</head>

<body>
<table id="orgList"></table>

<div id="orgAddWin" class="easyui-window" title="机构信息"
     style="padding:5px;width: 450px;height:450px;overflow: hidden;"
     iconCls="icon-search" closed="true" maximizable="false" minimizable="false"
     collapsible="false">
    <iframe frameborder="0" id="orgAddFrame" height="100%" width="100%" scrolling="yes"
            frameborder="0"></iframe>
</div>

<div id="orgDeptWin" class="easyui-window" title="机构弹出信息"
     style="padding:5px;float:left;top:10px;overflow: hidden;" iconCls="icon-search" closed="true"
     minimizable="false" maximizable="false" maximized="true" collapsible="false">
    <iframe frameborder="0" id="orgDeptFrame" height="100%" width="100%" scrolling="yes"
            frameborder="0"></iframe>
</div>
</body>
<script type="text/javascript" src="${ctx}/js/date.js"></script>
<script type="text/javascript" src="${ctx}/js/utiltools.js"></script>
</html>